import
  {
    getUserListApi,
    addUserListApi,
    updateGetUserListApi,
    updateUserListApi,
    deleteUserListApi,
    getUserDataApi,
    setUserDataApi,
    getUserMenuApi,
    setUserMenuApi,
  }
from '@/api/userManage'

import { getDSNListApi } from '@/api/DSNmanage';
import { getRoleListApi } from '@/api/roleManage';

const userManage  = {
  namespaced: true,
  state: {
    userList: [],
    currentConfig:{
      current : 1,
      pageSize: 10,
      total: 50,
      pageSizeOptions: ['10', '30', '50', '100'],
      showQuickJumper: true,
      showSizeChanger: true,
      showTotal: (total) => {
        let pageConfig = userManage.state.currentConfig
        return `共 ${total} 条记录 第${pageConfig.current} / ${Math.ceil(total/pageConfig.pageSize)} 页`
      }
    },
    order: null,
    orderBy: null,
    orgTreeData: [],
    replaceFields: {
      children:'children',
      title:'name',
      key:'id',
      value: 'id'
    },
  },
  mutations: {
    SET_USERLIST: (state, list) => {
      state.userList = list
    },
    SET_TOTAL: (state, total) => {
      state.currentConfig.total = total
    },
    SET_CURRENT: (state, current) => {
      state.currentConfig.current = current
    },
    SET_PAGE: (state, pages) => {
      state.currentConfig.pageSize = pages
    },
    SET_ORDER: (state, {order, orderBy}) => {
      state.order = order
      state.orderBy = orderBy
    },
    setOrgTreeData: (state, data) => {
      state.orgTreeData = data
    },
  },
  actions: {
    // 获取角色列表
    getAllRoleList ({ state, commit }) {
      return new Promise((resolve, reject) => {
        getRoleListApi()
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          }).catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 获取数据源列表
    getDSNList ({ state, commit }) {
      return new Promise((reslove, reject) => {
        getDSNListApi()
          .then(res => {
            if (res.code == 0) {
              reslove(res)
            } else {
              reject(res)
            }
          })
          .catch(err => {
            console.error(err)
            reject(err)
          })
      })
    },
    // 查询用户列表
    getUserList ({ state, commit }) {
      const pageSize = state.currentConfig.pageSize,
            page = state.currentConfig.current,
            order = state.order,
            orderBy = state.orderBy
      return new Promise((resolve, reject) => {
        getUserListApi({ pageSize, page, order, orderBy })
          .then(res => {
            if (res.code ==0) {
              const { data } = res
              commit('SET_USERLIST', data.data)
              commit('SET_TOTAL', data.total)
              commit('SET_CURRENT', data.current)
              commit('SET_PAGE', data.size)
              resolve(res)
            } else {
              reject(res)
            }

          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 创建用户 和 更新用户
    addUserList ({commit}, form) {
      return new Promise((resolve, reject) => {
        addUserListApi(form)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    updateUser ({}, form) {
      return new Promise((resolve, reject) => {
        updateUserListApi(form)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })

    },
    // 获取更新用户
    updateUserList ({commit}, id) {
      return new Promise((resolve, reject) => {
        updateGetUserListApi({ id })
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 删除用户
    deleteUserList ({commit} ,id) {
      return new Promise((resolve, reject) => {
        deleteUserListApi({id})
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 查询用户数据源权限
    getUserData ({commit}, id) {
      return new Promise((resolve, reject) => {
        getUserDataApi({id})
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 分配用户数据源权限
    setUserData ({commit}, data) {
      return new Promise((resolve, reject) => {
        setUserDataApi(data)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 获取用户菜单权限
    getUserMenu ({commit}, id) {
      return new Promise((resolve, reject) => {
        getUserMenuApi({id})
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
    // 分配用户菜单权限
    setUserMenu ({commit}, menu) {
      return new Promise((resolve, reject) => {
        setUserMenuApi(menu)
          .then(res => {
            if (res.code == 0) {
              resolve(res)
            } else {
              reject(res)
            }
          })
          .catch(error => {
            console.error(error)
            reject(error)
          })
      })
    },
  },
  getters: {

  }
}

export default userManage
